package com.tf_cut.backend.agent.service;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tf_cut.backend.agent.pojo.Agent;
import com.tf_cut.backend.agent.repository.AgentRepository;
import com.tf_cut.backend.agent.vo.AgentAndAccountVo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.util.UUIDUtil;

@Service
@Transactional
public class AgentServiceImpl extends CommonServiceImpl<AgentAndAccountVo, Agent, String> implements AgentService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private AgentRepository agentRepository;
    @Autowired
    private AgentAccountService agentAccountService;
    
	@Override
	public Result<AgentAndAccountVo> insertAgentAndAccount(AgentAndAccountVo avo) {
		
		avo.setAgentAccount(UUIDUtil.getUUID());
		Result<AgentAndAccountVo> result = this.save(avo);	// 新增代理商
		avo.setAgentId(result.getData().getAgentId());
		
		result = agentAccountService.save(avo);
		avo.setAgentAccountId(result.getData().getAgentAccountId());	// 新建虚拟账号
		
		result.setData(avo);
		
		return result;
	}

	@Override
	public Result<AgentAndAccountVo> updateAgentAndAccount(AgentAndAccountVo avo) {
		agentAccountService.save(avo);
		return this.save(avo);
	}
}
